//
//  TicketManger.swift
//  AirlineReservation
//
//  Created by Kevin on 2020/1/18.
//  Copyright © 2020 Kevin. All rights reserved.
//

import UIKit

class TicketMangerView: UIView {
    
    // 功能
    private var ticketviews = [TicketView]()
    // 回调
    public var complete: ((_ item:Int) -> Void)?
    // 文本
    private let titles = ["机票预订","改签票","退票","在线选座"]
    // 图标
    private let pics = ["ticket1","ticket2","ticket3","ticket4"]
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        init_view()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func init_view() -> Void {
        (0...3).forEach { (i) in
            ticketviews.append(TicketView())
        }
    }
    
    override func layoutSubviews() {
        if ticketviews.count == 0 {
            return
        }
        // 摆放位置
        let ticketW = frame.width / 4
        for i in 0..<pics.count {
            let ticket = ticketviews[i]
            ticket.tag = i
            // 赋值
            ticket.text = titles[i]
            ticket.image = UIImage(named: pics[i])
            ticket.frame = CGRect(x: CGFloat(i) * ticketW, y: MARGIN5, width: ticketW, height: frame.height)
            addSubview(ticket)
            // 添加点击事件
            ticket.addTarget(self, action: #selector(clickItem(btn:)), for: .touchUpInside)
        }
    }
    
    @objc func clickItem(btn:UIButton) -> Void {
        if let callback = complete {
            callback(btn.tag)
        }
    }
}
